//
//  WmcMediaTeamManagerAPI.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/9/15.
//

import Foundation
import RxSwift
import YPNetwork

struct WmcMediaTeamManagerAPI {
    
    /// 加入团队的方式
    enum JoinTeamType: Int {
        //扫二维码
        case scanQrCode = 0
        //微信
        case wechat = 1
        //手动添加
        case manualAdd = 2
        //团队号搜索
        case teamIdSearch = 3
    }
    
    /// 同步配置列表
    /// - DocumentUrl: http://yapi.3pvr.com/project/219/interface/api/2846
    /// - Returns: Observable<[WmcMediaAlbumConfigurationModel]>
    static func fetchConfiguration() -> Observable<[WmcMediaAlbumConfigurationModel]> {
        return WmcMediaRequest.requestData(path: "/wmc/v1/teamAlbum/listSyncConfig") { request in
            request.method = .post
        }.parseListModelWithClass(WmcMediaAlbumConfigurationModel.self)
    }

    /// 同步配置列表
    /// - DocumentUrl: http://yapi.3pvr.com/project/219/interface/api/2846
    /// - Parameters:
    ///   - success: ([WmcMediaAlbumConfigurationModel]) -> Void
    ///   - failure: (Error) -> Void
    static func fetchConfiguration(success: @escaping ([WmcMediaAlbumConfigurationModel]) -> Void, failure: @escaping (Error) -> Void) {
        return WmcMediaRequest.requestData(path: "/wmc/v1/teamAlbum/listSyncConfig") { request in
            request.method = .post
        } success: { (response: YPNetworkResponse) in
            if let dict = response.data as? [String: Any], let list = dict["list"] as? [Any] {
                let model = list.parseListModelWithClass(WmcMediaAlbumConfigurationModel.self)
                success(model)
            }
        } failure: { error in
            failure(error)
        }
    }

    /// 同步配置修改
    /// - DocumentUrl: http://yapi.3pvr.com/project/219/interface/api/2851
    /// - Parameters:
    ///   - albumId: 同步相册id
    ///   - isOpen: 是否开启
    ///   - teamId: 团队id
    /// - Returns: Observable<YPNetworkResponse>
    static func updateConfiguration(albumId: Int, isOpen: Bool, teamId: Int? = nil) -> Observable<YPNetworkResponse> {
        var params: [String: Any] = ["syncAlbumId": albumId, "isOpen": isOpen]

        if let teamId = teamId {
            params["teamId"] = teamId
        }
        return WmcMediaRequest.requestData(path: "/wmc/v1/teamAlbum/editSyncConfig", parameters: params) { request in
            request.method = .post
        }
    }

    /// 添加团队
    /// - DocumentUrl: http://yapi.3pvr.com/project/219/interface/api/3056
    /// - Parameter name:团队名称
    /// - Returns: Observable<YPNetworkResponse>
    static func createTeam(name: String) -> Observable<WmcCreateTeamResult> {
        let params: [String: Any] = ["teamName": name]
        return WmcMediaRequest.requestData(path: "/wmc/v1/team/add", parameters: params) { request in
            request.method = .post
        }.parseModelWithClass(WmcCreateTeamResult.self)
    }

    /// 修改团队名称
    /// - DocumentUrl: http://yapi.3pvr.com/project/219/interface/api/3081
    /// - Parameters:
    ///   - id: id
    ///   - name: name
    /// - Returns: Observable<YPNetworkResponse>
    static func updateTeamName(id: String, name: String) -> Observable<YPNetworkResponse> {
        let params: [String: Any] = ["teamId": id, "teamName": name]
        return WmcMediaRequest.requestData(path: "/wmc/v1/team/update", parameters: params) { request in
            request.method = .post
        }
    }

    /// 根据团队号查询团队信息
    /// - DocumentUrl: http://yapi.3pvr.com/project/219/interface/api/3091
    /// - Parameter teamOpenId: 团队号
    /// - Returns: Observable<WmcMediaTeamInfoModel>
    static func fetchTeamInfo(teamOpenId: String? = nil) -> Observable<WmcMediaTeamInfoModel> {
        var params: [String: Any] = [:]
        if let teamOpenId = teamOpenId {
            params["teamOpenId"] = teamOpenId
        }
        return WmcMediaRequest.requestData(path: "/wmc/v1/team/search", parameters: params) { request in
            request.method = .post
        }.parseModelWithClass(WmcMediaTeamInfoModel.self)
    }

    /// 获取团队详情
    /// - DocumentUrl: http://yapi.3pvr.com/project/219/interface/api/3361
    /// - Parameter teamId: 团队id
    /// - Returns: Observable<WmcMediaTeamDetailModel>
    static func fetchTeamInfo(teamId: Int) -> Observable<WmcMediaTeamDetailModel> {
        let params: [String: Any] = ["teamId": teamId]
        return WmcMediaRequest.requestData(path: "/wmc/v1/team/getTeamInfo", parameters: params) { request in
            request.method = .post
        }.parseModelWithClass(WmcMediaTeamDetailModel.self)
    }

    /// 成员列表
    /// - DocumentUrl: http://yapi.3pvr.com/project/219/interface/api/3096
    /// - Parameter id: 团队id
    /// - Returns: Observable<WmcMediaTeamInfoModel.WmcMediaTeamMemberModel>
    static func fetchMemberList(id: Int) -> Observable<[WmcTeamMemberModel]> {
        let params: [String: Any] = ["teamId": id]
        return WmcMediaRequest.requestData(path: "/wmc/v1/team-member/list", parameters: params) { request in
            request.method = .post
        }.parseListModelWithClass(WmcTeamMemberModel.self, designatedPath: "memberList")
    }

    /// 成员信息编辑
    /// - DocumentUrl: http://yapi.3pvr.com/project/219/interface/api/3101
    /// - Parameters:
    ///   - teamId: 团队id
    ///   - memberId: 成员id
    ///   - remark: 成员昵称
    /// - Returns: Observable<YPNetworkResponse>
    static func updateMemberInfo(teamId: Int? = nil, memberId: Int? = nil, remark: String? = nil) -> Observable<YPNetworkResponse> {
        var params: [String: Any] = [:]
        if let teamId = teamId {
            params["teamId"] = teamId
        }
        if let memberId = memberId {
            params["memberId"] = memberId
        }
        if let remark = remark {
            params["remark"] = remark
        }
        return WmcMediaRequest.requestData(path: "/wmc/v1/team-member/edit", parameters: params) { request in
            request.method = .post
        }
    }

    /// 删除成员
    /// - DocumentUrl: http://yapi.3pvr.com/project/219/interface/api/3106
    /// - Parameters:
    ///   - teamId: 团队id
    ///   - memberId: 团队成员id
    /// - Returns: Observable<YPNetworkResponse>
    static func deleteMember(teamId: Int? = nil, memberId: Int? = nil) -> Observable<YPNetworkResponse> {
        var params: [String: Any] = [:]
        if let teamId = teamId {
            params["teamId"] = teamId
        }
        if let memberId = memberId {
            params["memberId"] = memberId
        }
        return WmcMediaRequest.requestData(path: "/wmc/v1/team-member/delete", parameters: params) { request in
            request.method = .post
        }
    }

    /// 添加成员（手动添加）
    /// - DocumentUrl: http://yapi.3pvr.com/project/219/interface/api/3111
    /// - Parameters:
    ///   - teamId: 团队id
    ///   - mobile: 手机号
    ///   - remark: 备注名
    /// - Returns: Observable<YPNetworkResponse>
    static func addMember(teamId: Int, mobile: String, remark: String) -> Observable<YPNetworkResponse> {
        let params: [String: Any] = ["teamId": teamId, "mobile": mobile, "remark": remark]
        return WmcMediaRequest.requestData(path: "/wmc/v1/team-member/add-by-creator", parameters: params) { request in
            request.method = .post
        }
    }

    /// 添加成员（团队号添加）
    /// - DocumentUrl: http://yapi.3pvr.com/project/219/interface/api/3116
    /// - Parameter teamId: 团队id
    /// - Returns: Observable<YPNetworkResponse>
    static func addMember(teamId: Int? = nil,type: JoinTeamType = .scanQrCode) -> Observable<WmcCreateTeamResult> {
        var params: [String: Any] = [:]
        if let teamId = teamId {
            params["teamId"] = teamId
            params["type"] = type.rawValue
        }
        return WmcMediaRequest.requestData(path: "/wmc/v1/team-member/add-by-member", parameters: params) { request in
            request.method = .post
        }.parseModelWithClass(WmcCreateTeamResult.self)
    }

    /// 获取成员信息
    /// - DocumentUrl: http://yapi.3pvr.com/project/219/interface/api/3246
    /// - Parameters:
    ///   - teamId: 团队成员id
    ///   - memberId: 团队id
    ///   - yupaoId: 鱼泡Id
    /// - Returns: Observable<YPNetworkResponse>
    static func fetchMemberInfo(teamId: Int? = nil, memberId: Int? = nil, yupaoId: Int? = nil) -> Observable<WmcTeamMemberDetailModel> {
        var params: [String: Any] = [:]
        if let teamId = teamId {
            params["teamId"] = teamId
        }
        if let memberId = memberId {
            params["memberId"] = memberId
        }
        if let yupaoId = yupaoId {
            params["yupaoId"] = yupaoId
        }
        return WmcMediaRequest.requestData(path: "/wmc/v1/team-member/detail", parameters: params) { request in
            request.method = .post
        }.parseModelWithClass(WmcTeamMemberDetailModel.self)
    }

    /// 成员离开团队
    /// - DocumentUrl: http://yapi.3pvr.com/project/219/interface/api/3271
    /// - Parameters:
    ///   - teamId: 团队id
    ///   - uid: uid
    /// - Returns: Observable<YPNetworkResponse>
    static func exitTeam(teamId: Int, uid: Int) -> Observable<YPNetworkResponse> {
        let params: [String: Any] = ["teamId": teamId, "uid": uid]
        return WmcMediaRequest.requestData(path: "/wmc/v1/team-member/leaveTeam", parameters: params) { request in
            request.method = .post
        }
    }

    
    
    /// 团队资源列表
    /// DocumentUrl: http://yapi.3pvr.com/project/219/interface/api/3126
    /// - Parameters:
    ///   - teamId: 团队id
    ///   - date: 日期 (yyyy-MM-dd 格式)
    /// - Returns: Observable<[WmcMediaTeamResourceModel]>
    static func fetchTeamResource(teamId: Int? = nil, date: String? = nil) -> Observable<[WmcMediaTeamResourceModel]> {
        var params: [String: Any] = [:]
        if let teamId = teamId {
            params["teamId"] = teamId
        }
        if let date = date {
            params["date"] = date
        }
        return WmcMediaRequest.requestData(path: "/wmc/v1/teamAlbum/teamAlbumResourceList", parameters: params) { request in
            request.method = .post
        }.parseListModelWithClass(WmcMediaTeamResourceModel.self)
    }

    /// 删除资源
    /// - DocumentUrl: http://yapi.3pvr.com/project/219/interface/api/3201
    /// - Parameters:
    ///   - resourceId: 图片主键id
    ///   - teamId: 团队id
    /// - Returns: Observable<YPNetworkResponse>
    static func deleteTeamResource(resourceId: Int? = nil, teamId: Int? = nil) -> Observable<YPNetworkResponse> {
        var params: [String: Any] = [:]
        params["albumType"] = "1"
        if let resourceId = resourceId {
            params["resourceId"] = resourceId
        }
        if let teamId = teamId {
            params["busId"] = teamId
        }
        return WmcMediaRequest.requestData(path: "/wmc/v1/resource/delete", parameters: params) { request in
            request.method = .post
        }
    }
}

extension WmcMediaTeamManagerAPI {
    
    /// 获取 团队邀请相关信息
    /// - Parameter teamId: teamId description
    /// - Returns: description
    static func getTeamInviationInfo(teamId: Int) -> Observable<WmcInviationInfoModel> {
        return WmcMediaRequest.requestData(path: "/wmc/v1/share/inviteTeamByWechat", parameters: ["teamId": teamId]) { req in
            req.method = .post
        }.parseModelWithClass(WmcInviationInfoModel.self)
    }
}
